﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_HuiBaoBiaoService : It_HuiBaoBiaoService
    {
        public readonly MyContext _Context;
        public t_HuiBaoBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }

        

        public int Add(t_HuiBaoBiao huibao)
        {
            _Context.t_HuiBaoBiao.Add(huibao);
            return  _Context.SaveChanges();
        }

        public t_HuiBaoBiao GetId(int id)
        {
            return  _Context.t_HuiBaoBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_HuiBaoBiao> GetList(int id)
        {
            return  _Context.t_HuiBaoBiao.Where(x => x.JieShouRen == id).OrderBy(x => x.HuiBaoZhuangTai).ToList();
        }

        public List<t_HuiBaoBiao> HuiBaoRenList(int id)
        {
            return _Context.t_HuiBaoBiao.Where(x => x.HuiBaoRen == id && x.HuiBaoZhuangTai == "未读").ToList();
        }

        public List<t_RenYuanBiao> GetRenGuanLi(int BuMenId)
        {
            return _Context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Include(x => x.t_ZhanDuiBiao).Where(x => x.t_GangWeiBiao.BuMenId == BuMenId).DistinctBy(x => x.t_GangWeiBiao.DengJi).OrderByDescending(x => x.t_GangWeiBiao.DengJi).ToList();
        }

        public List<t_RenYuanBiao> GetRenYuanzhi(int BuMenId, int GangWeiDengJi)
        {
            return _Context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Include(x => x.t_ZhanDuiBiao).Where(x => x.t_GangWeiBiao.BuMenId == BuMenId && x.t_GangWeiBiao.DengJi == GangWeiDengJi).ToList();
        }

        public List<t_HuiBaoBiao> BenGetList(int id)
        {
            return _Context.t_HuiBaoBiao.Where(x => x.HuiBaoRen == id).OrderBy(x => x.HuiBaoZhuangTai).ToList();
        }

        public string GetListWithFenYe(string fasongren, string jieshouren, string huibaoliexing, int start, int limit, string dir, int column)
        {
            var list = _Context.t_HuiBaoBiao.Include(x => x.HuiBaoRenXinXi).Include(x => x.JieShouRenXinXi).ToList();
            if (fasongren != "" && fasongren != null) 
            {
                list = list.Where(x => x.HuiBaoRenXinXi.XingMing.Contains(fasongren)).ToList();
            }
            if (jieshouren != "" && jieshouren != null)
            {
                list = list.Where(x => x.JieShouRenXinXi.XingMing.Contains(jieshouren)).ToList();
            }
            if (huibaoliexing != "0" && (huibaoliexing != null && huibaoliexing != ""))
            {
                list = list.Where(x => x.HuiBaoLeiXing == huibaoliexing).ToList();
            }

            for (int i = 0; i < list.Count; i++)
            {
                list[i].JieShouRenXinXi.t_GangWeiBiao = null;
                list[i].JieShouRenXinXi.t_ZhanDuiBiao = null;
                list[i].JieShouRenXinXi.t_ZhanQuBiao = null;
                DateTime huibaoshijianshuju = Convert.ToDateTime(list[i].HuiBaoShiJian);
                list[i].HuiBaoShiJian = huibaoshijianshuju.ToString("yy-MM-dd hh:mm:ss");
            }

            for (int i = 0; i < list.Count; i++)
            {
                list[i].HuiBaoRenXinXi.t_GangWeiBiao = null;
                list[i].HuiBaoRenXinXi.t_ZhanDuiBiao = null;
                list[i].HuiBaoRenXinXi.t_ZhanQuBiao = null;
            }

            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    break;
                case 3:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    break;
                case 4:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.HuiBaoShiJian)).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
